package com.gzz.orchid.context.domain.business;

import com.gzz.orchid.common.core.support.ContextBeanRegistry;
import com.gzz.orchid.common.core.tuple.OrderNo;
import com.gzz.orchid.context.domain.business.entity.ShipOrderDo;
import com.gzz.orchid.context.domain.business.entity.TradeOrderDo;
import com.gzz.orchid.context.infrastructure.definition.Status;
import com.gzz.orchid.context.infrastructure.support.serial.ShipOrderSerialNoGenerator;
import com.gzz.orchid.context.infrastructure.support.serial.TradeOrderSerialNoGenerator;

import java.time.LocalDateTime;

/**
 * 订单工厂
 *
 * @author higmd
 */
public class OrderFactory {
    private final TradeOrderSerialNoGenerator tradeOrderGenerator;
    private final ShipOrderSerialNoGenerator shipOrderGenerator;

    public OrderFactory() {
        tradeOrderGenerator = ContextBeanRegistry.getBean(TradeOrderSerialNoGenerator.class);
        shipOrderGenerator = ContextBeanRegistry.getBean(ShipOrderSerialNoGenerator.class);
    }

    public static OrderFactory builder() {
        return SingletonHolder.INSTANCE;
    }

    /**
     * 构建交易订单对象
     *
     * @return TradeOrderDo 返回初始化后的订单对象
     */
    public TradeOrderDo buildTradeOrder() {
        // 实例化订单对象
        TradeOrderDo orderDo = new TradeOrderDo();
        // 生成订单号并设置到订单对象中
        orderDo.setNo(new OrderNo(tradeOrderGenerator.generateSerialNo()));
        // 设置订单的创建时间为当前系统时间
        orderDo.setTimeAt(LocalDateTime.now());
        // 设置订单状态为创建状态
        orderDo.setStatus(Status.CREATED);
        // 返回初始化后的订单对象
        return orderDo;
    }

    /**
     * 创建一个发货单对象
     *
     * @return ShipOrderDo 返回初始化后的发货单对象
     */
    public ShipOrderDo buildShipOrder() {
        // 创建一个发货单对象
        ShipOrderDo shipOrderDo = new ShipOrderDo();
        // 设置发货单的订单号
        shipOrderDo.setNo(new OrderNo(shipOrderGenerator.generateSerialNo()));
        // 设置发货单的创建时间为当前系统时间
        shipOrderDo.setTimeAt(LocalDateTime.now());
        // 设置发货单的状态为创建中
        shipOrderDo.setStatus(Status.CREATED);
        // 返回初始化后的发货单对象
        return shipOrderDo;
    }

    private static class SingletonHolder {
        private static final OrderFactory INSTANCE = new OrderFactory();
    }
}
